﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;
using CardRegisterCollection.DBUtility;
using CardRegisterCollection.Model;

//Please add references
namespace CardRegisterCollection.DAL
{
    /// <summary>
    /// 数据访问类:card_type
    /// </summary>
    public partial class CardType
    {
        public CardType()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from card_type");
            strSql.Append(" where DeleteFlag = 0 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }

            return DbHelperOleDb.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.CardType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into card_type(");
            strSql.Append("name,ParentId,sort,Type,CreatedBy,CreatedAt,DeleteFlag, Id)");
            strSql.Append(" values (");
            strSql.Append("@name,@ParentId,@sort,@Type,@CreatedBy,@CreatedAt,@DeleteFlag, @Id)");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@name", OleDbType.VarChar,255),
                    new OleDbParameter("@ParentId", OleDbType.VarChar,255),
                    new OleDbParameter("@sort", OleDbType.Integer,4),
                    new OleDbParameter("@Type", OleDbType.VarChar,255),
                    new OleDbParameter("@CreatedBy", OleDbType.VarChar,255),
                    new OleDbParameter("@CreatedAt", OleDbType.Date),
                    new OleDbParameter("@DeleteFlag", OleDbType.Boolean,1),
                    //new OleDbParameter("@LastUpdatedBy", OleDbType.VarChar,255),
                    //new OleDbParameter("@LastUpdatedAt", OleDbType.Date),
                     new OleDbParameter("@Id", OleDbType.VarChar,255),
            };
         
            parameters[0].Value = model.name;
            parameters[1].Value = model.ParentId;
            parameters[2].Value = model.sort;
            parameters[3].Value = model.Type;
            parameters[4].Value = model.CreatedBy;
            parameters[5].Value = model.CreatedAt;
            parameters[6].Value = model.DeleteFlag;
            //parameters[7].Value = model.LastUpdatedBy;
            //parameters[8].Value = model.LastUpdatedAt;
            parameters[7].Value = model.Id;
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.CardType model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update card_type set ");
            strSql.Append("name=@name,");
            strSql.Append("ParentId=@ParentId,");
            strSql.Append("sort=@sort,");
            strSql.Append("Type=@Type,");
          
            strSql.Append("DeleteFlag=@DeleteFlag,");
            strSql.Append("LastUpdatedBy=@LastUpdatedBy,");
            strSql.Append("LastUpdatedAt=@LastUpdatedAt");
            strSql.Append(" where Id=@Id ");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@name", OleDbType.VarChar,255),
                    new OleDbParameter("@ParentId", OleDbType.VarChar,255),
                    new OleDbParameter("@sort", OleDbType.Integer,4),
                    new OleDbParameter("@Type", OleDbType.VarChar,255),
                     
                    new OleDbParameter("@DeleteFlag", OleDbType.Boolean,1),
                    new OleDbParameter("@LastUpdatedBy", OleDbType.VarChar,255),
                    new OleDbParameter("@LastUpdatedAt", OleDbType.Date),
                    new OleDbParameter("@Id", OleDbType.VarChar,50)};
            parameters[0].Value = model.name;
            parameters[1].Value = model.ParentId;
            parameters[2].Value = model.sort;
            parameters[3].Value = model.Type;
           
            parameters[4].Value = model.DeleteFlag;
            parameters[5].Value = CurrentLoginInfo.UserInfo.UserName;
            parameters[6].Value = DateTime.Now;
            parameters[7].Value = model.Id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from card_type ");
            strSql.Append(" where Id=@Id ");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@Id", OleDbType.VarChar,50)         };
            parameters[0].Value = Id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from card_type ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CardType GetModel(string Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,name,ParentId,sort,Type,CreatedBy,CreatedAt,DeleteFlag,LastUpdatedBy,LastUpdatedAt from card_type ");
            strSql.Append(" where Id=@Id ");
            OleDbParameter[] parameters = {
                    new OleDbParameter("@Id", OleDbType.VarChar,50)         };
            parameters[0].Value = Id;

            Model.CardType model = new Model.CardType();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.CardType DataRowToModel(DataRow row)
        {
            Model.CardType model = new Model.CardType();
            if (row != null)
            {
                if (row["Id"] != null)
                {
                    model.Id = row["Id"].ToString() ;
                }
                if (row["name"] != null)
                {
                    model.name = row["name"].ToString();
                }
                if (row["ParentId"] != null)
                {
                    model.ParentId =  row["ParentId"].ToString();
                }
                if (row["sort"] != null && row["sort"].ToString() != "")
                {
                    model.sort = int.Parse(row["sort"].ToString());
                }
                if (row["Type"] != null)
                {
                    model.Type = row["Type"].ToString();
                }
                if (row["CreatedBy"] != null)
                {
                    model.CreatedBy = row["CreatedBy"].ToString();
                }
                if (row["CreatedAt"] != null && row["CreatedAt"].ToString() != "")
                {
                    model.CreatedAt = DateTime.Parse(row["CreatedAt"].ToString());
                }
                if (row["DeleteFlag"] != null && row["DeleteFlag"].ToString() != "")
                {
                    if ((row["DeleteFlag"].ToString() == "1") || (row["DeleteFlag"].ToString().ToLower() == "true"))
                    {
                        model.DeleteFlag = true;
                    }
                    else
                    {
                        model.DeleteFlag = false;
                    }
                }
                if (row["LastUpdatedBy"] != null)
                {
                    model.LastUpdatedBy = row["LastUpdatedBy"].ToString();
                }
                if (row["LastUpdatedAt"] != null && row["LastUpdatedAt"].ToString() != "")
                {
                    model.LastUpdatedAt = DateTime.Parse(row["LastUpdatedAt"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,name,ParentId,sort,Type,CreatedBy,CreatedAt,DeleteFlag,LastUpdatedBy,LastUpdatedAt ");
            strSql.Append(" FROM card_type ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM card_type ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Id desc");
            }
            strSql.Append(")AS Row, T.*  from card_type T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			OleDbParameter[] parameters = {
					new OleDbParameter("@tblName", OleDbType.VarChar, 255),
					new OleDbParameter("@fldName", OleDbType.VarChar, 255),
					new OleDbParameter("@PageSize", OleDbType.Integer),
					new OleDbParameter("@PageIndex", OleDbType.Integer),
					new OleDbParameter("@IsReCount", OleDbType.Boolean),
					new OleDbParameter("@OrderType", OleDbType.Boolean),
					new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
					};
			parameters[0].Value = "card_type";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

